function BubbleSort(arr) {
    let m = arr.length - 1,
        flag = 1;
    while ((m >= 0) && (flag === 1)) {
        flag = 0;
        for (let j = 0; j <= m; j++) {
            if (arr[j] > arr[j + 1]) {
                flag = 1;
                [arr[j], arr[j+1]] = [arr[j+1], arr[j]];
            }
        }
        m--;
    }

    return arr;
}

const arr = [49, 38, 65, 97, 76, 13, 27, 49];

arr = BubbleSort(arr);

console.log(arr)